package com.example.zg3_park

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.example.lib_base.mvvm.BaseViewActivity
import com.example.zg3_park.databinding.ActivityStartBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.*
import kotlin.concurrent.timerTask

@AndroidEntryPoint
class StartActivity : BaseViewActivity<ActivityStartBinding>() {
    var timer: Timer?=null
    var time = 5

    override fun intiView() {
        if(SPUtils.getInstance().getBoolean("time",false)) {
            ARouter.getInstance().build("/app/LoginActivity")
                .navigation()
            finish()
        }
        timer = Timer()
        //放于子线程
        lifecycleScope.launch(Dispatchers.IO) {
            timer !!.schedule(timerTask {
                lifecycleScope.launch {
                    binding.tvTime.setText("${time}秒")
                    if(time == 0) {
                        start()
                    }
                    time--
                }
            },0,1000)
        }

        binding.tvGo.setOnClickListener {
            start()

        }
    }

    private fun start() {
        timer!!.cancel()
        SPUtils.getInstance().put("time",true)
        ARouter.getInstance().build("/app/LoginActivity")
            .navigation()
        finish()
    }

    override fun intiData() {

    }
}